package sangpil.temp
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import gs.TweenLite;
	import gs.easing.*;
	
	import sangpil.temp.event.EasingSetEvent;

	[Event( name="changeItem", type="sangpil.temp.event.EasingSetEvent")]
	public class EasingSet extends Sprite
	{
		private var rect:Sprite = new Sprite();
		private var txtBox:TextField = new TextField();
		private var cb:Sprite = new Sprite();
		private var sX:Number = 150;
		private var tX:Number = 450;
		private var time:Number = 1;
		private var w:Number = 15;
		private var h:Number = 15;
		private var loop:Boolean = true;
		
		private var inFnc:Function;
		
		public function EasingSet($label:String, $ease:Function)
		{
			super();
			this.inFnc = $ease;
			this.setDisplay($label, $ease);
			this.addEvent();
		}
		
		public function trigger():void
		{
			this.onDown();
		}

		private function setDisplay($label:String, $ease:Function):void
		{
			var format1:TextFormat = new TextFormat();
		    format1.font="Arial";
		    format1.size=12;
			
			this.cb.graphics.beginFill(0xffffff, 1);
			this.cb.graphics.drawRect(0, 0, w, h);
			this.cb.graphics.endFill();
			this.cb.x = 15;
			this.cb.alpha = 0.3;
			this.addChild(this.cb);
			
			this.txtBox.height = 20;
			this.txtBox.width = 100;
			this.txtBox.x = 35;
			this.txtBox.text = $label;
			this.txtBox.textColor = 0xffffff;
			this.txtBox.setTextFormat(format1);
			this.addChild(this.txtBox);
			
			this.rect.graphics.beginFill(0xffffff, 1);
			this.rect.graphics.drawRect(0, 0, w, h);
			this.rect.graphics.endFill();
			this.rect.alpha = 0.01;
			
			this.addChild(this.rect);
			this.rect.x = sX;
			
			TweenLite.to(this.rect, this.time, {x:tX, ease:$ease, delay:2,  onComplete:onFinishTween, onCompleteParams:[2, this.rect, $ease]});
		}
		
		
		private function onFinishTween(arg1:Number, arg2:Sprite, arg3:Function):void
		{
			if(loop)
			{
				var curTx:Number;
				if(this.rect.x > sX)
				{
					curTx = sX;
				}else{
					curTx = tX;
				}
				
				TweenLite.to(this.rect, this.time, {x:curTx, ease:arg3, delay:2,  onComplete:onFinishTween, onCompleteParams:[2, this.rect, arg3]});
			}
			
		}
		/* 
		TweenLite.to(clip_mc, 5, {alpha:0.5, x:120, ease:Back.easeOut, delay:2, onComplete:onFinishTween, onCompleteParams:[5, clip_mc]});
		function onFinishTween(argument1:Number, argument2:MovieClip):void {
			trace("The tween has finished! argument1 = " + argument1 + ", and argument2 = " + argument2);
		}
		 */
		
		private function addEvent():void
		{
			this.cb.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
		}
		private var CR:String = String.fromCharCode (13); // Enter keyCode
		private var onoff:Boolean = true;
		private function onDown(e:MouseEvent = null):void
		{
			if(onoff)
			{
				//e.currentTarget.alpha = 0.5;
				this.cb.transform.colorTransform = new ColorTransform(1,0,0,1,0,0,0,0);
				this.rect.transform.colorTransform = new ColorTransform(1,0,0,1,0,0,0,0);
				onoff = false;
				
				var codetxt:String = '';
				codetxt += 'Return formula : ';
				switch(this.inFnc)
				{
					case Back.easeIn:
					codetxt += 'Back.easeIn, s = 1.70158'+CR+CR;
					codetxt += 'return c*(t/=d)*t*((s+1)*t - s) + b;';
					break;
					case Back.easeOut:
					codetxt += 'Back.easeOut, s = 1.70158'+CR+CR;
					codetxt += 'return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;';
					break;
					case Back.easeInOut:
					codetxt += 'Back.easeInOut, s = 1.70158'+CR+CR;
					codetxt += 'if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;'
							 + CR + 'return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;';
					break;
					
					case Bounce.easeIn:
					codetxt += 'Bounce.easeIn'+CR+CR;
					codetxt += 'if ((t/=d) < (1/2.75)) {'+CR
								+'return c*(7.5625*t*t) + b;'+CR
								+'} else if (t < (2/2.75)) {'+CR
								+'	return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;'+CR
								+'} else if (t < (2.5/2.75)) {'+CR
								+'	return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;'+CR
								+'} else {'+CR
								+'	return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;'+CR
								+'}';
					break;
					case Bounce.easeOut:
					codetxt += 'Bounce.easeOut'+CR+CR;
					codetxt += 'return c - easeOut(d-t, 0, c, d) + b;';
					break;
					case Bounce.easeInOut:
					codetxt += 'Bounce.easeInOut'+CR+CR;
					codetxt += 'if (t < d/2) return easeIn (t*2, 0, c, d) * .5 + b;'+CR+
								'else return easeOut (t*2-d, 0, c, d) * .5 + c*.5 + b;';
					break;
					
					case Circ.easeIn:
					codetxt += 'Circ.easeIn'+CR+CR;
					codetxt += 'return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;';
					break;
					case Circ.easeOut:
					codetxt += 'Circ.easeOut'+CR+CR;
					codetxt += 'return c * Math.sqrt(1 - (t=t/d-1)*t) + b;';
					break;
					case Circ.easeInOut:
					codetxt += 'Circ.easeInOut'+CR+CR;
					codetxt += 'if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;'+CR
								'return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;';
					break;
					
					case Cubic.easeIn:
					codetxt += 'Cubic.easeIn'+CR+CR;
					codetxt += 'return c*(t/=d)*t*t + b;';
					break;
					case Cubic.easeOut:
					codetxt += 'Cubic.easeOut'+CR+CR;
					codetxt += 'return c*((t=t/d-1)*t*t + 1) + b;';
					break;
					case Cubic.easeInOut:
					codetxt += 'Cubic.easeInOut'+CR+CR;
					codetxt += 'if ((t/=d/2) < 1) return c/2*t*t*t + b;'+CR+
								'return c/2*((t-=2)*t*t + 2) + b;';
					break;
					
					case Elastic.easeIn:
					codetxt += 'Elastic.easeIn, a = 0, p = 0'+CR+CR;
					codetxt += ''+
								'var _2PI:Number = Math.PI * 2;'+CR+
								'var s:Number;'+CR+CR+
								'if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;'+CR+
								'if (!a || a < Math.abs(c)) { a=c; s = p/4; }'+CR+
								'else s = p/_2PI * Math.asin (c/a);'+CR+
								'return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*_2PI/p )) + b;'
					;
					break;
					case Elastic.easeOut:
					codetxt += 'Elastic.easeOut, a = 0, p = 0'+CR+CR;
					codetxt += ''+
								'var _2PI:Number = Math.PI * 2;'+CR+
								'var s:Number;'+CR+CR+
								'if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;'+CR+
								'if (!a || a < Math.abs(c)) { a=c; s = p/4; }'+CR+
								'else s = p/_2PI * Math.asin (c/a);'+CR+
								'return (a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*_2PI/p ) + c + b);'					
					
					;
					break;
					case Elastic.easeInOut:
					codetxt += 'Elastic.easeInOut, a = 0, p = 0'+CR+CR;
					codetxt += ''+
								'var _2PI:Number = Math.PI * 2;'+CR+
								'var s:Number;'+CR+CR+
								'if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);'+CR+
								'if (!a || a < Math.abs(c)) { a=c; s = p/4; }'+CR+
								'else s = p/_2PI * Math.asin (c/a);'+CR+
								'if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*_2PI/p )) + b;'+CR+
								'return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*_2PI/p )*.5 + c + b;'
					;
					break;
					
					case Expo.easeIn:
					codetxt += 'Expo.easeIn'+CR+CR;
					codetxt += 'return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b - c * 0.001;';
					break;
					case Expo.easeOut:
					codetxt += 'Expo.easeOut'+CR+CR;
					codetxt += 'return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;';
					break;
					case Expo.easeInOut:
					codetxt += 'Expo.easeInOut'+CR+CR;
					codetxt += ''+
								'if (t==0) return b;'+CR+
								'if (t==d) return b+c;'+CR+
								'if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;'+CR+
								'return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;'
					;
					break;
					
					case Linear.easeIn:
					codetxt += 'Linear.easeIn'+CR+CR;
					codetxt += 'return c*t/d + b;';
					break;
					case Linear.easeOut:
					codetxt += 'Linear.easeOut'+CR+CR;
					codetxt += 'return c*t/d + b;';
					break;
					case Linear.easeInOut:
					codetxt += 'Linear.easeInOut'+CR+CR;
					codetxt += 'return c*t/d + b;';
					break;
					case Linear.easeNone:
					codetxt += 'Linear.easeNone'+CR+CR;
					codetxt += 'return c*t/d + b;';
					break;
					
					case Quad.easeIn:
					codetxt += 'Quad.easeIn'+CR+CR;
					codetxt += 'return c*(t/=d)*t + b;';
					break;
					case Quad.easeOut:
					codetxt += 'Quad.easeOut'+CR+CR;
					codetxt += 'return -c *(t/=d)*(t-2) + b;';
					break;
					case Quad.easeInOut:
					codetxt += 'Quad.easeInOut'+CR+CR;
					codetxt += ''+
								'if ((t/=d/2) < 1) return c/2*t*t + b;'+CR+
								'return -c/2 * ((--t)*(t-2) - 1) + b;'
					;
					break;
					
					case Quart.easeIn:
					codetxt += 'Quart.easeIn'+CR+CR;
					codetxt += 'return c*(t/=d)*t*t*t + b;';
					break;
					case Quart.easeOut:
					codetxt += 'Quart.easeOut'+CR+CR;
					codetxt += 'return -c * ((t=t/d-1)*t*t*t - 1) + b;';
					break;
					case Quart.easeInOut:
					codetxt += 'Quart.easeInOut'+CR+CR;
					codetxt += ''+
								'if ((t/=d/2) < 1) return c/2*t*t*t*t + b;'+CR+
								'return -c/2 * ((t-=2)*t*t*t - 2) + b;'
					;
					break;
					
					case Quint.easeIn:
					codetxt += 'Quint.easeIn'+CR+CR;
					codetxt += 'return c*(t/=d)*t*t*t*t + b;';
					break;
					case Quint.easeOut:
					codetxt += 'Quint.easeOut'+CR+CR;
					codetxt += 'return c*((t=t/d-1)*t*t*t*t + 1) + b;';
					break;
					case Quint.easeInOut:
					codetxt += 'Quint.easeInOut'+CR+CR;
					codetxt += ''+
								'if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;'+CR+
								'return c/2*((t-=2)*t*t*t*t + 2) + b;'
					;
					break;
					
					case Sine.easeIn:
					codetxt += 'Sine.easeIn'+CR+CR;
					codetxt += 'private static const _HALF_PI:Number = Math.PI / 2;'+CR+
								'return -c * Math.cos(t/d * _HALF_PI) + c + b;'
					;
					break;
					case Sine.easeOut:
					codetxt += 'Sine.easeOut'+CR+CR;
					codetxt += 'private static const _HALF_PI:Number = Math.PI / 2;'+CR+
								'return c * Math.sin(t/d * _HALF_PI) + b;'
					;
					break;
					case Sine.easeInOut:
					codetxt += 'Sine.easeInOut'+CR+CR;
					codetxt += 'private static const _HALF_PI:Number = Math.PI / 2;'+CR+
								'return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;'
					;
					break;
					
					case Strong.easeIn:
					codetxt += 'Strong.easeIn'+CR+CR;
					codetxt += 'return c*(t/=d)*t*t*t*t + b;';
					break;
					case Strong.easeOut:
					codetxt += 'Strong.easeOut'+CR+CR;
					codetxt += 'return c*((t=t/d-1)*t*t*t*t + 1) + b;';
					break;
					case Strong.easeInOut:
					codetxt += 'Strong.easeInOut'+CR+CR;
					codetxt += ''+
								'if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;'+CR+
								'return c/2*((t-=2)*t*t*t*t + 2) + b;'
					;
					break;
					
				}
				
				codetxt += CR+CR+CR + 'Parameter description';
				codetxt += '     >>>    t : current Time   d : total Time    c : amplitude = 1    b : base line = 0'+CR+CR ;
				
				codetxt +=  'Usuage   >>>   object : target to move    reV : return value    distance : distance'
							+CR +'object.x  = object.x + (reV * distance);'

				
				this.dispatchEvent(new EasingSetEvent(codetxt, EasingSetEvent.CHANGE_ITEM));
				//this.dispatchEvent(new SpComboBoxEvent(this.combo.selectedItem.label, this.combo.selectedItem.data, SpComboBoxEvent.CHANGE_LABEL));
				
			}else{
				//e.currentTarget.alpha = 1;
				this.cb.transform.colorTransform = new ColorTransform(1,1,1,0.3,0,0,0,0);
				this.rect.transform.colorTransform = new ColorTransform(1,1,1,0.01,0,0,0,0);
				onoff = true;
			}
			
			
		}
		private function setDefault():void
		{
			
		}

		
	}
}